package com.cdc.animation;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

public class AnimatorSetActivity extends AppCompatActivity {

    private ImageView imgBlue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator_set);

        imgBlue = (ImageView) findViewById(R.id.imgBlue);
    }

    public void togetherRun(View view) {
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(imgBlue, "scaleX",
                1.0f, 2f);
        anim1.setRepeatCount(ObjectAnimator.INFINITE);
        anim1.setRepeatMode(ObjectAnimator.RESTART);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(imgBlue, "scaleY",
                1.0f, 2f);

        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "translationX",
                0.0f, 350.0f, 0f);
        anim2.setRepeatCount(ObjectAnimator.INFINITE);
        anim2.setRepeatMode(ObjectAnimator.REVERSE);
        anim3.setRepeatCount(ObjectAnimator.INFINITE);
        anim3.setRepeatMode(ObjectAnimator.REVERSE);

        Log.d("123",anim2.getPropertyName());

        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        //两个动画同时执行
   //     animSet.playTogether(anim1, anim2);
		animSet.playSequentially(anim1, anim2,anim3);
        animSet.start();
    }

    public void playWithAfter(View view) {
        float cx = imgBlue.getX();

        ObjectAnimator anim1 = ObjectAnimator.ofFloat(imgBlue, "scaleX",
                1.0f, 2f);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(imgBlue, "scaleY",
                1.0f, 2f);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue,
                "x",  cx ,  0f);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(imgBlue,
                "x", cx);

        /**
         * anim1，anim2,anim3同时执行
         * anim4接着执行
         */
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(anim1).with(anim2);
        animSet.play(anim2).with(anim3);
        animSet.play(anim4).after(anim3);
        animSet.setDuration(1000);
        animSet.start();
    }

    public void translationX(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "translationX",
                0.0f, 350.0f);
        //anim3.setRepeatCount(1);
        //anim3.setRepeatMode(ObjectAnimator.RESTART);

        ObjectAnimator anim4 = ObjectAnimator.ofFloat(imgBlue, "translationX",
                350.0f,-505.0f,0.0f);

        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3,anim4);
        animSet.start();


    }

    public void translationY(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "translationY",
                0.0f, 350.0f);
        //anim3.setRepeatCount(1);
        //anim3.setRepeatMode(ObjectAnimator.RESTART);

        ObjectAnimator anim4 = ObjectAnimator.ofFloat(imgBlue, "translationY",
                350.0f,-505.0f,0.0f);

        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3,anim4);
        animSet.start();
    }



    public void translationXYTo(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "translationX",
                0.0f, 350.0f);


        ObjectAnimator anim4 = ObjectAnimator.ofFloat(imgBlue, "translationX",
                350.0f,-505.0f,0.0f);

        ObjectAnimator anim3Y = ObjectAnimator.ofFloat(imgBlue, "translationY",
                0.0f, 350.0f);


        ObjectAnimator anim4Y = ObjectAnimator.ofFloat(imgBlue, "translationY",
                350.0f,-505.0f,-100.0f);

        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playTogether(anim3,anim4,anim3Y,anim4Y);
        animSet.start();
    }

    public void translationXYSe(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "translationX",
                0.0f, 350.0f);


        ObjectAnimator anim4 = ObjectAnimator.ofFloat(imgBlue, "translationX",
                350.0f,-505.0f,0.0f);

        ObjectAnimator anim3Y = ObjectAnimator.ofFloat(imgBlue, "translationY",
                0.0f, 350.0f);


        ObjectAnimator anim4Y = ObjectAnimator.ofFloat(imgBlue, "translationY",
                350.0f,-505.0f,-100.0f);

        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3,anim4,anim3Y,anim4Y);
        animSet.start();
    }

    public void rotation(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "rotation",
                0.0f, 90.0f,0.0f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3);
        animSet.start();
    }



    public void rotationX(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "rotationX",
                0.0f, 90.0f,0.0f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3);
        animSet.start();
    }

    public void rotationY(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "rotationY",
                0.0f, 92.0f,0.0f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3);
        animSet.start();
    }

    public void scaleX(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "scaleX",
                1.0f, 3.0f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3);
        animSet.start();
    }

    public void scaleY(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "scaleY",
                1.0f, 3.0f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3);
        animSet.start();
    }

    public void pointX(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "pivotX",
                0.0f, 350.0f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3);
        animSet.start();

    }

    public void pointY(View view) {
    }

    public void alpha(View view) {
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgBlue, "alpha",
                1.0f, 0.3f,0.1f,0.5f,1.0f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.setDuration(2000);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playSequentially(anim3);
        animSet.start();
    }
}
